package com.gateway.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.gateway.common.enums.ResultEnum;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Huangkai
 */
public class AjaxResult extends HashMap<String, Object> {
    public final static String CODE = "code";
    public final static String DATA = "data";
    public final static String MESSAGE = "message";

    public static AjaxResult success(){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put(CODE, 200);
        ajaxResult.put(MESSAGE, "操作成功");
        return ajaxResult;
    }

    public static AjaxResult error(){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put(CODE, 201);
        ajaxResult.put(MESSAGE, "操作失败");
        return ajaxResult;
    }

    /**
     * 返回结果【只携带消息】
     * */
    public static AjaxResult resultWithMessage(Object code, String message){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put(CODE, code);
        ajaxResult.put(MESSAGE, message);
        return ajaxResult;
    }
    /**
     * 枚举类自定义状态码返回
     * */
    public static AjaxResult ResultWithMessageEnum(ResultEnum resultEnum){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put(CODE, resultEnum.getCode());
        ajaxResult.put(MESSAGE, resultEnum.getMessage());
        return ajaxResult;
    }
    /**
     * 枚举类带数据的状态码返回
     * */
    public static AjaxResult ResultWithMessageDataEnum(ResultEnum resultEnum, Object data){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put(CODE, resultEnum.getCode());
        ajaxResult.put(MESSAGE, resultEnum.getMessage());
        ajaxResult.put(DATA, data);
        return ajaxResult;
    }

    /**
     * 枚举类带数据不带消息返回
     * */
    public static AjaxResult ResultWithDataSuccess(Object data){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put(CODE, 200);
        ajaxResult.put(DATA, data);
        return ajaxResult;
    }
}
